package com.aries.ui.widget.demo.adapter;


import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.entity.TitleEntity;
import com.aries.ui.widget.demo.entity.WidgetEntity;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;
import java.util.Optional;

/**
 * @Author: AriesHoo on 2019/7/17 15:00
 * @E-Mail: AriesHoo@126.com
 * @Function: 条目效果
 * @Description:
 */

public class WidgetAdapter extends BaseItemProvider {
    private List<WidgetEntity> widgetList;
    private Context context;
    private DirectionalLayout mHeaderLayout;
    private boolean mHeadAndEmptyEnable = false;

    public WidgetAdapter(Context context, List<WidgetEntity> widgetList) {
        this.context = context;
        this.widgetList = widgetList;
    }

    @Override
    public int getCount() {
        if (mHeaderLayout != null) {
            return widgetList == null ? 1 : widgetList.size() + 1;
        }
        return widgetList == null ? 0 : widgetList.size();
    }

    @Override
    public Object getItem(int i) {
        if (mHeaderLayout != null) {
            if(i == 0){
                return null;
            }else{
                return widgetList.get(i - 1);
            }
        }
        return widgetList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component componentP, ComponentContainer componentContainer) {
        if (mHeaderLayout != null && position == 0) {
            return mHeaderLayout;
        }

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_widget, null, false);
        ViewHolder viewHolder = new ViewHolder();
        Component componentTitle = component.findComponentById(ResourceTable.Id_tv_titleWidget);
        Component componentcontent = component.findComponentById(ResourceTable.Id_tv_contentWidget);
        if (componentTitle instanceof Text) {
            viewHolder.title = (Text) componentTitle;
        }
        if (componentcontent instanceof Text) {
            viewHolder.content = (Text) componentcontent;
        }
        component.setTag(viewHolder);

        if(mHeaderLayout != null){
            viewHolder.title.setText(widgetList.get(position - 1).title);
            viewHolder.content.setText(widgetList.get(position - 1).content);
        }else{
            viewHolder.title.setText(widgetList.get(position).title);
            viewHolder.content.setText(widgetList.get(position).content);
        }

        return component;
    }

    public void addHeaderView(Component header, int index, int orientation) {
        mHeadAndEmptyEnable = true;
        if (mHeaderLayout == null) {
            mHeaderLayout = new DirectionalLayout(header.getContext());
            int MATCH_PARENT = DirectionalLayout.LayoutConfig.MATCH_PARENT;
            int MATCH_CONTENT = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
            if (orientation == DirectionalLayout.VERTICAL) {
                mHeaderLayout.setOrientation(DirectionalLayout.VERTICAL);
                mHeaderLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(MATCH_PARENT, MATCH_CONTENT));
            } else {
                mHeaderLayout.setOrientation(DirectionalLayout.HORIZONTAL);
                mHeaderLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(MATCH_CONTENT, MATCH_PARENT));
            }
        }
        final int childCount = mHeaderLayout.getChildCount();
        if (index < 0 || index > childCount) {
            index = childCount;
        }
        mHeaderLayout.addComponent(header, index);
        if (mHeaderLayout.getChildCount() == 1) {
            int position = getHeaderViewPosition();
            if (position != -1) {
                notifyDataSetItemInserted(position);
            }
        }
    }

    private int getHeaderViewPosition() {
        if (getEmptyViewCount() == 1) {
            if (mHeadAndEmptyEnable) {
                return 0;
            }
        } else {
            return 0;
        }
        return -1;
    }

    public int getEmptyViewCount() {
        if (widgetList.size() != 0) {
            return 0;
        }
        return 1;
    }

    private static class ViewHolder {
        Text title;
        Text content;
    }
}
